<?php

namespace app\modules\api\controllers;

use app\models\MiniprogramSession;
use app\models\User;
use app\models\UserAttr;
use Yii;
use yii\wxcrypt\WXBizDataCrypt;
use app\modules\api\CommonController;
require_once(Yii::$app->basePath.'/vendor/wxcrypt/WXBizDataCrypt.php');
require_once(Yii::$app->basePath.'/vendor/wxcrypt/ErrorCode.php');



class WxController extends CommonController
{
    //解密并保存手机号
    public function actionPhone(){
        try{
            $encryptedData = Yii::$app->request->post('encryptedData','');
            $errMsg = Yii::$app->request->post('errMsg','');
            $iv = Yii::$app->request->post('iv','');
            $err_arr = explode(':',$errMsg);
            if($err_arr[1]!='ok') throw new \Exception('获取加密消息失败',1);
            if(empty($this->session)) throw new \Exception('获取SESSION失败',1);
            $pc = new WXBizDataCrypt('wx945baaf3dfb83a32', $this->session->session_key);
            $errCode = $pc->decryptData($encryptedData, $iv, $data );
            if ($errCode != 0) throw new \Exception('微信解密错误:'.$errCode,1);
            $data = json_decode($data,1);
            $this->user->phone = $data['phoneNumber'];
            if(!$this->user->save())throw new \Exception(json_encode($this->user->getErrors(),JSON_UNESCAPED_UNICODE),1);;
            return $this->success();
        }catch (\Exception $e){
            return $this->error($e);
        }
    }
    //解密并保存手机号
    public function actionUserInfo(){
        try{
            $encryptedData = Yii::$app->request->post('encryptedData','');
            $errMsg = Yii::$app->request->post('errMsg','');
            $iv = Yii::$app->request->post('iv','');
            $err_arr = explode(':',$errMsg);
            if($err_arr[1]!='ok') throw new \Exception('获取加密消息失败',1);
            if(empty($this->session)) throw new \Exception('获取SESSION失败',1);
            $pc = new WXBizDataCrypt('wx945baaf3dfb83a32', $this->session->session_key);
            $errCode = $pc->decryptData($encryptedData, $iv, $data );
            if ($errCode != 0) throw new \Exception('微信解密错误:'.$errCode,1);
            $data = json_decode($data,1);
            $this->user->head_url = $data['avatarUrl'];
            $this->user->city = $data['city'];
            $this->user->country = $data['country'];
            $this->user->province = $data['province'];
            $this->user->sex = $data['gender'];
            $this->user->nickname = $data['nickName'];
            if(!$this->user->save())throw new \Exception(json_encode($this->user->getErrors(),JSON_UNESCAPED_UNICODE),1);

            return $this->success();
        }catch (\Exception $e){
            return $this->error($e);
        }
    }
    //是否保存过用户信息
    public function actionIssetUserinfo(){
        try{
            $session = MiniprogramSession::getObjBySid($this->sid);
            if(empty($session)) throw new \Exception('获取SESSION失败',1);
            $userAttr = UserAttr::findOne(['attr_id'=>UserAttr::OPENID_MINI,'attr_data'=>$session->openid]);
            if(!$userAttr){
                return $this->success(['isset'=>0]);
            }
            $userAttr = UserAttr::findOne(['uid'=>$userAttr->uid,'attr_id'=>UserAttr::HEAD]);
            if(!$userAttr){
                return $this->success(['isset'=>0]);
            }
            return $this->success(['isset'=>1]);
        }catch (\Exception $e){
            return $this->error($e);
        }
    }
    //是否保存过用户手机
    public function actionIssetPhone(){
        try{
            $session = MiniprogramSession::getObjBySid($this->sid);
            if(empty($session)) throw new \Exception('获取SESSION失败',1);
            $userAttr = UserAttr::findOne(['attr_id'=>UserAttr::OPENID_MINI,'attr_data'=>$session->openid]);
            if(!$userAttr){
                return $this->success(['isset'=>0]);
            }
            $userAttr = UserAttr::findOne(['uid'=>$userAttr->uid,'attr_id'=>UserAttr::PHONE]);
            if(!$userAttr){
                return $this->success(['isset'=>0]);
            }
            return $this->success(['isset'=>1]);
        }catch (\Exception $e){
            return $this->error($e);
        }
    }
    //是否保存过用户个人资料
    public function actionIssetData(){
        try{
            $userAttr = UserAttr::findOne(['uid'=>$this->getUid(),'attr_id'=>UserAttr::REAL_NAME]);
            if(!$userAttr){
                return $this->success(['isset'=>0]);
            }
            return $this->success(['isset'=>1]);
        }catch (\Exception $e){
            return $this->error($e);
        }
    }
}
